﻿
@{
    Layout = null;
}



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>特殊钢材料价格设置</title>
    <link href="~/Content/style.css" rel="stylesheet" type="text/css" />
    <style>
        .pd_left{
            width:280px;
            float:left;
        }
        h3{
            margin:0;
        }
    </style>
</head>

<body style="background:#f5f5f5; height:100%">
    @Html.Action("Top","Home")

    <div class="clear"></div>

    <div id="product">
        <div class="pd_left">
            <div class="pro_l">
                <ul style="background:none; display:block;">
                    <li>
                        <p><a href="javascript:;" target="main" data-level="0" data-id="0">我的目录</a></p><em></em><i></i>
                        <ul style="display:block;">
                            
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
        <div class="pro_c" style="width:140px; float:left;">
            <a class="add" onclick="onAddSameItem();">添加同级项目</a>
            <a class="add" onclick="onAddChildItem();">添加下级项目</a>
            <a onclick="onDeleteItem();">删除选择项目</a>
            <a class="add3" onclick="onCopyProduct();">复制整个产品</a>
        </div>
        <div>
            <iframe name="main" src="/Product/list" scrolling="No" frameborder="0"></iframe>
        </div>
    </div>

    <!--添加产品-->
    <div id="box_add" class="div_productadd">
        <h2>添加产品<span>×</span></h2>
        <div><span>产品编号：</span><input type="text" name="Sno" required data-type="text" maxlength="50" data-error="产品编号不能为空且长度不能超过50个字符！" /></div>
        <div><span>产品名称：</span><input type="text" name="Name" required data-type="text" maxlength="50" data-error="产品名称不能为空且长度不能超过50个字符！"></div>
        <div><span>客户姓名：</span><input type="text" name="Customer" data-type="text" maxlength="20" data-error="客户姓名长度不能超过20个字符！"></div>
        <div><span>订单编号：</span><input type="text" name="OrderNo" data-type="text" maxlength="50" data-error="订单编号长度不能超过50个字符！"></div>
        <p><input type="submit" class="jb" value="保存" onclick="onAddProduct();"/><input class="jb" type="button" value="返回"></p>
    </div>
    <!--添加零件-->
    <div id="box_add" class="div_partadd">
        <h2>添加零件信息<span>×</span></h2>
        <div><span>零件编号：</span><input style="width:200px;" type="text" /><a class="jb">>></a></div>
        <div><span>零件名称：</span><input type="text"></div>
        <p><input type="submit" class="jb" value="保存" /><input class="jb" type="button" value="返回"></p>
    </div>

    <!--产品信息拷贝-->
    <div id="box_add3">
        <h2>产品信息拷贝<span>×</span></h2>
        <div><span>原产品编号：</span><input type="text" /></div>
        <div><span>原产品名称：</span><input type="text"></div>
        <h3><span>目标产品信息</span></h3>
        <div><span>产品编号：</span><input type="text" /></div>
        <div><span>产品名称：</span><input type="text"></div>
        <div><span>客户名称：</span><input type="text" /></div>
        <div><span>订单编号：</span><input type="text"></div>
        <p><input type="submit" class="jb" value="保存" /><input class="jb" type="button" value="返回"></p>
    </div>

    <script type="text/javascript" src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script>
        $("iframe[name='main']").width($(window).width() - 460);
        $("iframe[name='main']").height($(window).height() - 80);
    </script>
    <script src="~/Scripts/menu.js"></script>
    <script src="~/Scripts/uilts.js"></script>
    <script>
        $(function () {
            InitTree();
            $('#box_add').find('p input').eq(1).click(function () {
                $('#box_add').css("display", "none");
            });
            $('#box_add').find('h2 span').click(function () {
                $('#box_add').css("display", "none");
            });
        });

        function InitTreeEvent() {
            $('.pro_l p').click(function () {
                $('.pro_l p').removeClass('active');
                $(this).addClass('active');

                var selected = GetSelectItem();
                if (selected.id == -1) return;
                if (selected.level == 0) {
                    $("iframe[name='main']").attr("src", "/Product/list");
                } else if (selected.level == 1) {
                    $("iframe[name='main']").attr("src", "/Product/Partlist?partid=" + selected.partid + '&productid=' + selected.productid);
                } else if (selected.level == 2) {
                    $("iframe[name='main']").attr("src", "/Product/Materiallist?partid=" + selected.partid + '&productid=' + selected.productid);
                }
            });
            $('.pro_l i').attr('onoff', 1);
            $('.pro_l i').click(function () {
                if ($(this).attr('onoff') == 1) {
                    $(this).addClass('on');
                    $(this).next().css("display", "block");
                    $(this).attr('onoff', 0);
                } else {
                    $(this).removeClass('on');
                    $(this).next().css("display", "none");
                    $(this).attr('onoff', 1);
                }
            });
        }
    </script>
    <script>
        function InitTree() {
            PostApi('/Product/GetTree', { product_id : 0 }, function (result) {
                if (result.code == 0) {
                    $("div#product>div.pd_left>div.pro_l>ul>li:first>ul").empty();
                    var html = '';
                    $.each(result.data, function (index, value) {
                        var html_product = '<p><a href="javascript:;" data-level ="1" data-id="' + value.product_id + '" target="' + value.sno + '">' + value.name + '<b>（未录完）</b></a></p><i></i>';
                        var html_part = "";
                        if (value.Parts != undefined && value.Parts.length > 0) {
                            $.each(value.Parts, function (itm_index, itm_v) {
                                html_part += '<li><p><a href="javascript:;" data-level="2" data-id="' + itm_v.id + '" target="' + itm_v.sno + '">' + itm_v.name + '</a></p><span></span></li>';
                            });
                        }
                        html_part = '<ul>' + html_part + '</ul>';
                        html += '<li>' + html_product + html_part + '</li>';
                    });
                    $("div#product>div.pd_left>div.pro_l>ul>li:first>ul").append(html);
                    InitTreeEvent();
                } else {
                    alert(result.ErrorMessage == undefined ? "产品树初始化失败！" : result.ErrorMessage);
                }
            }, function () {

            });
        }

        function onAddSameItem() {

        }

        function onAddChildItem() {
            var st = GetSelectItem();
            if (st.id == -1) return;
            if (st.level == 0) {
                //添加产品
                $("div.div_productadd").css("display","block");
            } else if (st.level == 1) {
                //添加零件
                $("div.div_partadd").css("display", "block");
            }
        }

        function onDeleteItem() {

        }

        function onCopyProduct() {

        }

        function GetSelectItem(){
            var selectP = $("#product p.active>a");
            if (selectP == undefined || selectP.length == 0)
                alert("未选中");

            var id = selectP.attr("data-id")||-1;
            var level = selectP.attr("data-level") || -1;
            var productid = 0, partid = 0;
            if (level == 1)
                productid = id;
            else if (level == 2)
            {
                //selectP.parents("")
                partid = id;
            }

            return {
                productid: productid,
                partid: partid,
                id: id,
                level: level
            }
        }

        function onAddProduct() {
            if (!ValidateModel("div.div_productadd", function (error) {
                alert(error);
            })) {
                return;
            };
            var data = GetFormData("div.div_productadd");
            PostApi("/Product/AddProduct", JSON.stringify(data), function (result) {
                if (result.code == 0) {
                    alert("添加成功!");
                    EmptyForm("div.div_productadd");
                    InitTree();
                } else {
                    alert(result.ErrorMessage);
                }
            }, function () {
                alert("添加失败!");
            }, "application/json");
        }

    </script>

</body>
</html>